<template>
  <div class="main-container">
    <a-tabs type="line" v-model="key" @change="changeTopTabs" auto-switch>
      <a-tab-pane title="限速方案" key="0">
        <TableBody>
          <template #header>
            <TableHeader ref="tableHeaderRef" :show-filter="false">
              <template #search-content>
                <a-form ref="serchForm" :model="form" :style="{ width: '100%' }">
                  <a-row :gutter="24">
                    <a-col :span="4">
                      <a-form-item field="name" hide-label>
                        <a-input v-model="form.name" placeholder="请输入方案名称" allow-clear />
                      </a-form-item>
                    </a-col>
                    <!-- <a-col :span="4">
                  <a-form-item field="state" hide-label>
                    <a-select v-model="form.close" placeholder="请选择方案状态" allow-clear>
                      <a-option value="0">正常</a-option>
                      <a-option value="1">关闭</a-option>
                    </a-select>
                  </a-form-item>
                </a-col> -->
                    <a-col :span="4">
                      <a-form-item field="addTime" hide-label>
                        <a-date-picker
                          style="width: 100%"
                          v-model="form.addTime"
                          show-time
                          placeholder="请选择开始创建时间"
                          :shortcuts="[
                            {
                              label: '今日',
                              value: () => (
                                (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                                  'YYYY-MM-DD HH:mm:ss'
                                )),
                                (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                                  'YYYY-MM-DD HH:mm:ss'
                                ))
                              ),
                            },
                            {
                              label: '昨日',
                              value: () => (
                                (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                                  .subtract(1, 'day')
                                  .format('YYYY-MM-DD HH:mm:ss')),
                                (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                                  .subtract(1, 'day')
                                  .format('YYYY-MM-DD HH:mm:ss'))
                              ),
                            },
                            {
                              label: '七日',
                              value: () => (
                                (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                                  .subtract(7, 'day')
                                  .format('YYYY-MM-DD HH:mm:ss')),
                                (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                                  .subtract(1, 'day')
                                  .format('YYYY-MM-DD HH:mm:ss'))
                              ),
                            },
                            {
                              label: '本月',
                              value: () => (
                                (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                                  'YYYY-MM-DD HH:mm:ss'
                                )),
                                (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                              ),
                            },
                            {
                              label: '上月',
                              value: () => (
                                (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                                  .subtract(1, 'month')
                                  .format('YYYY-MM-DD HH:mm:ss')),
                                (form.endTime = $dayjs()
                                  .subtract(1, 'month')
                                  .endOf('month')
                                  .format('YYYY-MM-DD HH:mm:ss'))
                              ),
                            },
                          ]"
                          :time-picker-props="{ defaultValue: '12:00:00' }"
                          format="YYYY-MM-DD HH:mm:ss"
                          allow-clear
                        />
                      </a-form-item>
                    </a-col>
                    <a-col :span="4">
                      <a-form-item field="endTime" hide-label>
                        <a-date-picker
                          style="width: 100%"
                          v-model="form.endTime"
                          show-time
                          placeholder="请选择开始创建时间"
                          :shortcuts="[
                            {
                              label: '今日',
                              value: () => (
                                (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                                  'YYYY-MM-DD HH:mm:ss'
                                )),
                                (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                                  'YYYY-MM-DD HH:mm:ss'
                                ))
                              ),
                            },
                            {
                              label: '昨日',
                              value: () => (
                                (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                                  .subtract(1, 'day')
                                  .format('YYYY-MM-DD HH:mm:ss')),
                                (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                                  .subtract(1, 'day')
                                  .format('YYYY-MM-DD HH:mm:ss'))
                              ),
                            },
                            {
                              label: '七日',
                              value: () => (
                                (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                                  .subtract(7, 'day')
                                  .format('YYYY-MM-DD HH:mm:ss')),
                                (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                                  .subtract(1, 'day')
                                  .format('YYYY-MM-DD HH:mm:ss'))
                              ),
                            },
                            {
                              label: '本月',
                              value: () => (
                                (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                                  'YYYY-MM-DD HH:mm:ss'
                                )),
                                (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                              ),
                            },
                            {
                              label: '上月',
                              value: () => (
                                (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                                  .subtract(1, 'month')
                                  .format('YYYY-MM-DD HH:mm:ss')),
                                (form.endTime = $dayjs()
                                  .subtract(1, 'month')
                                  .endOf('month')
                                  .format('YYYY-MM-DD HH:mm:ss'))
                              ),
                            },
                          ]"
                          :time-picker-props="{ defaultValue: '12:00:00' }"
                          format="YYYY-MM-DD HH:mm:ss"
                          allow-clear
                        />
                      </a-form-item>
                    </a-col>
                    <a-col :span="4">
                      <a-form-item field="btns" hide-label>
                        <a-space :size="15">
                          <a-button
                            type="primary"
                            html-type="submit"
                            v-permission="getLimitList"
                            @click="doRefresh()"
                            ><template #icon>
                              <icon-search />
                            </template>
                            <!-- Use the default slot to avoid extra spaces -->
                            <template #default>搜索</template></a-button
                          >
                          <a-button v-permission="getLimitList" @click="resetSeach"
                            ><template #icon>
                              <icon-sync theme="outline" size="14" />
                            </template>
                            <!-- Use the default slot to avoid extra spaces -->
                            <template #default>重置</template></a-button
                          >
                        </a-space>
                      </a-form-item>
                    </a-col>
                  </a-row>
                </a-form>
              </template>
            </TableHeader>
          </template>
          <template #table-config>
            <a-space :size="15">
              <a-button type="primary" v-permission="addLimit" @click="showAdd">
                <!-- Use the default slot to avoid extra spaces -->
                <template #default>新建</template>
              </a-button>
            </a-space>
            <a-space :size="15">
              <TableConfig
                @update-border="onUpdateBorder"
                @update-striped="onUpdateStriped"
                @refresh="doRefresh"
              />
            </a-space>
          </template>
          <template #default>
            <a-table
              :loading="tableLoading"
              :bordered="{ wrapper: true, cell: tables.bordered }"
              :stripe="(tables.striped as boolean)"
              :data="table.dataList"
              :pagination="false"
              :row-key="rowKey"
              :row-class-name="rowClassNameFun"
            >
              <template #columns>
                <a-table-column
                  v-for="item of tableColumns.list"
                  :key="item.key"
                  align="left"
                  :title="(item.title as string)"
                  :width="item.width"
                  :data-index="(item.key as string)"
                  :fixed="item.fixed"
                >
                  <template v-if="item.key === 'userRealName'" #cell="{ record }">
                    <span v-if="record.userRealName">
                      <a-button style="padding: 0px 4px" type="text" size="small"> 已实名</a-button>
                    </span>
                    <span v-else>
                      <a-button style="padding: 0px 4px" type="text" size="small">未实名</a-button>
                    </span>
                  </template>
                  <template v-else-if="item.key === 'step'" #cell="{ record }">
                    <span v-if="record.step == 0"> 按消耗总流量 </span>
                    <span v-else> 按当日消耗流量 </span>
                  </template>
                  <template v-else-if="item.key === 'series'" #cell="{ record }">
                    <span style="cursor: pointer" @click="seriesList(record.series)">
                      {{ record.series.length }}</span
                    >
                  </template>
                  <template v-else-if="item.key === 'speedLimitTypeId'" #cell="{ record }">
                    <span
                      v-for="(items, index) in typeLists"
                      :key="index"
                      v-show="items.id == record.speedLimitTypeId"
                      >{{ items.name }}{{ item.id }}</span
                    >
                  </template>
                  <template v-else-if="item.key === 'actions'" #cell="{ record }">
                    <a-space :size="2">
                      <a-button
                        style="padding: 0px 4px"
                        type="text"
                        size="small"
                        v-permission="updateLimit"
                        @click="updatUserPop(record)"
                        >编辑</a-button
                      >
                      <a-button
                        style="padding: 0px 4px"
                        type="text"
                        size="small"
                        v-permission="addLimit"
                        @click="onCopy(record)"
                        >复制</a-button
                      >
                      <a-button
                        style="padding: 0px 4px"
                        type="text"
                        size="small"
                        v-permission="delLimit"
                        @click="onDeleteItem(record)"
                        >删除</a-button
                      >
                    </a-space>
                  </template>
                </a-table-column>
              </template>
            </a-table>
          </template>
        </TableBody>
      </a-tab-pane>
      <a-tab-pane title="标签方案" key="1">
        <div class="containBox">
          <div class="tabelLeft">
            <div class="tabTitle">
              <span class="title">方案类型</span>
              <div class="btns">
                <a-button style="padding: 0px 4px" type="text" size="small" @click="showAddTags"
                  >新建</a-button
                >
                <a-button
                  style="padding: 0px 4px"
                  status="success"
                  type="text"
                  @click="showPop"
                  size="small"
                  >设置</a-button
                >
              </div>
            </div>
            <div class="tagList">
              <div
                class="activeTag"
                v-for="(item, index) of typeListsDatas"
                :key="index"
                @click="changeTagType(item.id)"
              >
                <span
                  class="actives"
                  @contextmenu.prevent.stop="onContextMenu(item, $event)"
                  v-if="activeTypeId == item.id"
                >
                  <span class="wraps">
                    {{ item.name }}
                  </span>
                </span>
                <span
                  class="noactive"
                  @contextmenu.prevent.stop="onContextMenu(item, $event)"
                  v-else
                >
                  <span class="wraps">
                    {{ item.name }}
                  </span></span
                >
              </div>
            </div>
            <!-- <a-tabs
              position="left"
              :hide-content="true"
              v-model="activeTypeId"
              @change="changeTagType"
            >
              <a-tab-pane v-for="item of typeListsDatas" style="height: 44px" :key="item.id">
                <template #title> </template>
              </a-tab-pane>
            </a-tabs> -->
          </div>
          <TableBody style="margin-top: -20px; flex-grow: 1">
            <template #table-config>
              <a-space :size="15">
                <a-button type="primary" @click="addTypeTags">
                  <template #icon>
                    <icon-plus />
                  </template>
                  <template #default>新建</template>
                </a-button>
              </a-space>
              <a-space :size="15">
                <TableConfig
                  @update-border="onUpdateBorder"
                  @update-striped="onUpdateStriped"
                  @refresh="getTagList(activeTypeId)"
                />
              </a-space>
            </template>
            <template #default>
              <a-table
                :loading="loadingTabel"
                :bordered="{ wrapper: true, cell: tables.bordered }"
                :stripe="(tables.striped as boolean)"
                :data="lists"
                :pagination="false"
                :row-key="rowKey"
                :row-class-name="rowClassNameFun"
              >
                <template #columns>
                  <a-table-column
                    v-for="item of tagColumns.list"
                    :key="item.key"
                    align="left"
                    :title="(item.title as string)"
                    :width="item.width"
                    :data-index="(item.key as string)"
                    :fixed="item.fixed"
                  >
                    <template v-if="item.key === 'actions'" #cell="{ record }">
                      <a-space :size="2">
                        <a-button
                          style="padding: 0px 4px"
                          type="text"
                          size="small"
                          @click="updateTags(record)"
                          >编辑</a-button
                        >
                        <a-button
                          style="padding: 0px 4px"
                          type="text"
                          size="small"
                          status="warning"
                          @click="delTags(record)"
                          >删除</a-button
                        >
                      </a-space>
                    </template>
                  </a-table-column>
                </template>
              </a-table>
            </template>
          </TableBody>
        </div>
      </a-tab-pane>
    </a-tabs>
    <a-modal
      v-model:visible="visible"
      @before-close="handleCancel"
      :title="addLimitTitle"
      width="700px"
      :mask-closable="false"
      title-align="start"
    >
      <a-form ref="userForm" :auto-label-width="true" :model="subForm">
        <a-form-item
          label="方案名称"
          field="name"
          :rules="[
            { required: true, message: '方案名称不能为空' },
            { minLength: 1, maxLength: 100, message: '方案名称必须小于100个字符' },
          ]"
          asterisk-position="end"
          :validate-trigger="['change', 'input']"
        >
          <a-input v-model.trim="subForm.name" placeholder="请输入方案名称" allow-clear />
        </a-form-item>
        <a-form-item field="speedLimitTypeId" label="限速渠道">
          <a-select
            v-model="subForm.speedLimitTypeId"
            placeholder="请选择限速渠道"
            @change="getTags"
            :disabled="selectTypes"
            allow-clear
          >
            <a-option v-for="(item, index) in typeLists" :key="index" :value="item.id">{{
              item.name
            }}</a-option>
          </a-select>
        </a-form-item>
        <a-form-item field="step" label="限速类型">
          <a-radio-group class="radio-bcg" v-model="subForm.step">
            <a-radio :value="0">
              <template #radio="{ checked }">
                <div style="display: flex" :class="{ 'custom-radio-card-checked': checked }">
                  <div className="custom-radio-card-mask">
                    <div className="custom-radio-card-mask-dot"></div> </div
                  ><div className="custom-radio-card-title">按消耗总流量</div>
                </div>
              </template>
            </a-radio>
            <a-radio :value="1">
              <template #radio="{ checked }">
                <div style="display: flex" :class="{ 'custom-radio-card-checked': checked }">
                  <div className="custom-radio-card-mask">
                    <div className="custom-radio-card-mask-dot"></div> </div
                  ><div className="custom-radio-card-title">按当日消耗流量</div>
                </div>
              </template>
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          v-for="(post, index) of subForm.speedLimitRules"
          :field="`posts.${index}.value`"
          label="阈值"
          :key="index"
        >
          <a-input-number
            v-model="post.usedFlow"
            :style="{ width: '320px' }"
            placeholder="请输入阈值"
            hide-button
          >
            <template #append>
              <a-button type="primary" @click="change1024(post.usedFlow, index)"> 换算 </a-button>
            </template>
          </a-input-number>
          <a-select
            style="margin-left: 15px; width: 200px"
            v-model="post.speedLimitTagId"
            placeholder="请选择方案标签"
            allow-clear
          >
            <a-option v-for="(items, indexs) in tagLists" :key="indexs" :value="items.id">{{
              items.name
            }}</a-option>
          </a-select>
          <i-add
            style="cursor: pointer; margin-left: 10px"
            v-if="index == 0"
            size="18"
            @click="addItem"
          />
          <i-reduce
            style="cursor: pointer; margin-left: 10px"
            v-else
            size="18"
            @click="handleDelete(index)"
          />
        </a-form-item>
      </a-form>
      <template #footer>
        <a-space>
          <a-button @click="handleCancel">取消</a-button>
          <a-button type="primary" @click="handleBeforeOk">确定</a-button>
        </a-space>
      </template>
    </a-modal>
    <a-modal
      v-model:visible="tagvisible"
      @before-close="handleCancelTag"
      :title="tagTypeTitle"
      width="600px"
      :mask-closable="false"
      title-align="start"
    >
      <a-form ref="userFormTag" :auto-label-width="true" :model="subFormTag">
        <a-form-item
          label="方案名称"
          field="name"
          :rules="[
            { required: true, message: '方案名称不能为空' },
            { minLength: 1, maxLength: 100, message: '方案名称必须小于100个字符' },
          ]"
          asterisk-position="end"
          :validate-trigger="['change', 'input']"
        >
          <a-input v-model.trim="subFormTag.name" placeholder="请输入方案名称" allow-clear />
        </a-form-item>
      </a-form>
      <template #footer>
        <a-space>
          <a-button @click="handleCancelTag">取消</a-button>
          <a-button type="primary" @click="handleBeforeOkTag">确定</a-button>
        </a-space>
      </template>
    </a-modal>
    <a-modal
      v-model:visible="shebeivisible"
      @before-close="handleCancelsb"
      title="限速标签"
      width="700px"
      :mask-closable="false"
      title-align="start"
    >
      <!-- <a-space :size="15" style="margin-bottom: 20px">
        <a-button type="primary" @click="addTagsPop">
          <template #icon>
            <icon-plus />
          </template>
          <template #default>新建</template>
        </a-button>
      </a-space> -->
      <a-spin :loading="typesLoading" tip="加载中····" style="width: 100%">
        <a-table
          :bordered="{ wrapper: true, cell: tables.bordered }"
          :stripe="(tables.striped as boolean)"
          :row-class-name="rowClassNameFun"
          :data="typeListsDatas"
          :pagination="false"
          :column-resizable="true"
          :rowKey="rowKey"
          table-layout-fixed
        >
          <template #columns>
            <a-table-column
              v-for="item of tableColumnsTypes.list"
              :key="item.key"
              align="left"
              :width="item.width"
              :title="(item.title as string)"
              :data-index="(item.key as string)"
            >
              <template v-if="item.key === 'actions'" #cell="{ record }">
                <a-space :size="2">
                  <a-button
                    style="padding: 0px 4px"
                    type="text"
                    size="small"
                    @click="updatePopType(record)"
                    >编辑</a-button
                  >

                  <a-button
                    style="padding: 0px 4px"
                    type="text"
                    size="small"
                    status="warning"
                    @click="delLimitTypes(record)"
                    >删除</a-button
                  >
                </a-space>
              </template>
              <template v-else-if="item.key === 'speedLimitTypeId'" #cell="{ record }">
                <span
                  v-for="(items, index) in typeLists"
                  :key="index"
                  v-show="items.id == record.speedLimitTypeId"
                  >{{ items.name }}</span
                >
              </template>
            </a-table-column>
          </template>
        </a-table>
      </a-spin>
      <template #footer>
        <!-- <a-space>
          <a-button @click="handleCancelsb">取消</a-button>
          <a-button type="primary" @click="handleBeforeOkBansb">确定</a-button>
        </a-space> -->
      </template>
    </a-modal>
    <a-modal
      v-model:visible="addtagvisible"
      @before-close="handleCancelTags"
      :title="tagsTitle"
      width="600px"
      :mask-closable="false"
      title-align="start"
    >
      <a-form ref="userFormTags" :auto-label-width="true" :model="subFormTags">
        <a-form-item
          label="标签名称"
          field="name"
          :rules="[
            { required: true, message: '方案名称不能为空' },
            { minLength: 1, maxLength: 100, message: '方案名称必须小于100个字符' },
          ]"
          asterisk-position="end"
          :validate-trigger="['change', 'input']"
        >
          <a-input v-model.trim="subFormTags.name" placeholder="请输入方案名称" allow-clear />
        </a-form-item>
        <a-form-item
          label="标签类型"
          field="type"
          :rules="[
            { required: true, message: '标签类型不能为空' },
            { minLength: 1, maxLength: 100, message: '标签类型必须小于100个字符' },
          ]"
          asterisk-position="end"
          :validate-trigger="['change', 'input']"
        >
          <a-input v-model.trim="subFormTags.type" placeholder="请输入标签类型" allow-clear />
        </a-form-item>
      </a-form>
      <template #footer>
        <a-space>
          <a-button @click="handleCancelTags">取消</a-button>
          <a-button type="primary" @click="handleBeforeOkTags">确定</a-button>
        </a-space>
      </template>
    </a-modal>
    <a-modal
      v-model:visible="addtagvisibles"
      title="应用系列"
      width="600px"
      :mask-closable="false"
      title-align="start"
    >
      <Table :tablist="series" />
      <template #footer>
        <a-space />
      </template>
    </a-modal>
    <ul v-show="showContextMenu" class="contex-menu-wrapper" :style="contextMenuStyle">
      <li>
        <a-button type="text" @click="updatUserPopTag"> 编辑 </a-button>
      </li>
      <li>
        <a-button status="warning" type="text" @click="delLimitTypes"> 删除 </a-button>
      </li>
    </ul>
  </div>
</template>

<script lang="ts" setup>
  import useCommonStore from '@/store/modules/common'
  import * as http from '@/api/http'
  import Table from '@/views/polling/compoents/table.vue'
  import {
    getLimitList,
    getLimitTypeList,
    getLimitTag,
    addLimit,
    addLimitType,
    updateLimitType,
    exportLimitList,
    updateLimit,
    delLimit,
    delLimitTypeTag,
    addLimitTypeTag,
    updateLimitTypeTag,
    delLimitType,
  } from '@/api/url'
  import { useRowKey, useTable } from '@/hooks/table'

  import { Form, Message, Modal } from '@arco-design/web-vue'
  import { onMounted, reactive, ref, watch } from 'vue'
  const useCommon = useCommonStore()
  const table = useTable()
  const rowKey = useRowKey('id')
  const visible = ref(false)
  const btnLoading = ref(false)
  const tagvisible = ref(false)
  const userForm = ref<typeof Form>()
  const userFormTag = ref<typeof Form>()
  const userFormTags = ref<typeof Form>()
  const shebeivisible = ref(false)
  const addtagvisible = ref(false)
  const addtagvisibles = ref(false)
  const loadingTabel = ref(false)
  const typesLoading = ref(false)
  const activeTypeId = ref('')
  const showContextMenu = ref(false)
  const selectTypes = ref(false)
  const form = reactive({
    name: '',
    addTime: '',
    endTime: '',
  })

  const subForm: any = ref({
    name: '',
    speedLimitTypeId: '',
    step: '',
    speedLimitRules: [
      {
        usedFlow: 0,
        speedLimitTagId: '',
      },
    ], //预存金额充值列表
  })
  const tagTypeTitle = ref('添加方案')
  const lists = ref([])
  const subFormTag: any = ref({
    name: '',
  })
  const series = ref([])
  const addLimitTitle = ref('添加方案')
  const tagsTitle = ref('添加标签')
  const subFormTags: any = ref({
    name: '',
    type: '',
    speedLimitTypeId: '',
  })

  const key = ref('0')
  const tagColumns: any = reactive({
    list: [
      {
        title: '标签名称',
        key: 'name',
        dataIndex: 'name',
        checked: true,
        width: 120,
      },
      {
        title: '标签类型',
        key: 'type',
        dataIndex: 'type',
        checked: true,
        width: 120,
      },
      {
        title: '操作',
        key: 'actions',
        dataIndex: 'actions',
        width: 180,
        align: 'center',
        checked: true,
      },
    ],
  })
  const tableColumnsTypes: any = reactive({
    list: [
      {
        title: '类型ID',
        key: 'id',
        dataIndex: 'id',
        checked: true,
      },
      {
        title: '类型名称',
        key: 'name',
        dataIndex: 'name',
        checked: true,
      },
      {
        title: '操作',
        key: 'actions',
        fixed: 'right',
        dataIndex: 'actions',
        width: 180,
        align: 'center',
        checked: true,
      },
    ],
  })
  const tableColumns: any = reactive({
    list: [
      {
        title: '方案名称',
        key: 'name',
        dataIndex: 'name',
        checked: true,
      },
      {
        title: '应用系列',
        key: 'series',
        dataIndex: 'series',
        checked: true,
      },
      {
        title: '限速类型',
        key: 'speedLimitTypeId',
        dataIndex: 'speedLimitTypeId',
        checked: true,
      },
      {
        title: '阶梯限速',
        key: 'step',
        dataIndex: 'step',
        checked: true,
      },

      {
        title: '创建时间',
        key: 'createTime',
        dataIndex: 'createTime',
        checked: true,
      },
      {
        title: '操作',
        key: 'actions',
        fixed: 'right',
        dataIndex: 'actions',
        width: 180,
        align: 'center',
        checked: true,
      },
    ],
  })
  const contextMenuStyle: any = reactive({
    left: '0',
    top: '0',
  })
  const typeLists: any = ref([])
  const typeListsDatas: any = ref([])

  const tagLists: any = ref([])
  const tableLoading = ref(false)
  const tables = reactive({
    tableLoading: false,
    bordered: false,
    striped: false,
    expand: [1],
    isExpand: false,
  })
  const change1024 = (item: any, value: any) => {
    subForm.value.speedLimitRules[value].usedFlow = item * 1024
  }
  function showAdd() {
    addLimitTitle.value = '添加方案'
    selectTypes.value = false
    subForm.value = {
      name: '',
      speedLimitTypeId: '',
      step: '',
      speedLimitRules: [
        {
          usedFlow: 0,
          speedLimitTagId: '',
        },
      ], //预存金额充值列表
      close: true,
    }
    tagLists.value = []
    visible.value = true
  }

  function seriesList(val: any) {
    series.value = val
    addtagvisibles.value = true
  }

  function showAddTags() {
    subFormTag.value = {
      name: '',
    }
    tagTypeTitle.value = '添加方案'
    tagvisible.value = true
  }
  function changeTagType(value: any) {
    activeTypeId.value = value
    getTagList(value)
  }
  function addTypeTags() {
    tagsTitle.value = '添加标签'
    subFormTags.value = {
      name: '',
      type: '',
      speedLimitTypeId: '',
    }
    addtagvisible.value = true
  }
  function updateTags(item: any) {
    subFormTags.value = JSON.parse(JSON.stringify(item))
    tagsTitle.value = '编辑标签'
    addtagvisible.value = true
  }
  function updatePopType(item: any) {
    tagTypeTitle.value = '编辑类型'
    subFormTag.value = JSON.parse(JSON.stringify(item))
    tagvisible.value = true
  }
  const addItem = () => {
    subForm.value.speedLimitRules.push({
      usedFlow: 0,
      speedLimitTagId: '',
    })
  }
  const handleDelete = (index: any) => {
    subForm.value.speedLimitRules.splice(index, 1)
  }

  function handleCancel() {
    userForm.value?.resetFields()
    visible.value = false
  }
  const handleCancelsb = () => {
    shebeivisible.value = false
  }
  // const handleBeforeOkBansb = () => {
  //   shebeivisible.value = false
  // }
  function handleCancelTag() {
    userFormTag.value?.resetFields()
    tagvisible.value = false
  }
  function handleCancelTags() {
    userFormTags.value?.resetFields()
    addtagvisible.value = false
  }
  function showPop() {
    shebeivisible.value = true
  }
  const serchForm = ref()
  const resetSeach = () => {
    serchForm.value.resetFields()
    doRefresh(1)
  }
  // function showTagsList(item: any) {
  //   console.log(item)
  //   getTagList(item.id)
  //   shebeivisible.value = true
  // }
  function gettypeList(val?: any) {
    typesLoading.value = true
    http
      .get({
        url: getLimitTypeList,
        data: () => {
          return {}
        },
      })
      .then((res) => {
        if (val == 1) {
          typeLists.value = res.data
        } else if (val == 2) {
          typeListsDatas.value = []
          res.data.forEach((item: any) => {
            if (item.system == false) {
              typeListsDatas.value.push(item)
            }
          })
          setTimeout(() => {
            let a = JSON.parse(JSON.stringify(typeListsDatas.value))
            typeListsDatas.value = a
          }, 100)
          typeListsDatas.value.forEach((item: any) => {
            if (item.id == activeTypeId.value) {
              lists.value = item.speedLimitTags
            }
          })
          typesLoading.value = false
        } else {
          typeListsDatas.value = []
          res.data.forEach((item: any) => {
            if (item.system == false) {
              typeListsDatas.value.push(item)
            }
          })
          activeTypeId.value = typeListsDatas.value[0].id
          setTimeout(() => {
            let a = JSON.parse(JSON.stringify(typeListsDatas.value))
            typeListsDatas.value = a
          }, 100)
          typeListsDatas.value.forEach((item: any) => {
            console.log(item.id)
            console.log(activeTypeId.value)
            if (item.id == activeTypeId.value) {
              lists.value = item.speedLimitTags
            }
          })
          typesLoading.value = false
        }
      })
  }
  function getTags(value: any) {
    typeLists.value.forEach((item: any) => {
      if (item.id == value) {
        tagLists.value = item.speedLimitTags
      }
    })
  }
  function getTagList(item: any) {
    tagLists.value = []
    loadingTabel.value = true
    http
      .get({
        url: getLimitTag,
        data: () => {
          return {
            speedLimitTypeId: item,
          }
        },
      })
      .then((res) => {
        tagLists.value = res.data
        loadingTabel.value = false
      })
  }
  function changeTopTabs(value: any) {
    if (value == 1) {
      gettypeList()
    }
  }
  function updatUserPop(item: any) {
    addLimitTitle.value = '编辑方案'
    selectTypes.value = true
    // getTagList(item.speedLimitTypeId)
    typeLists.value.forEach((itemw: any) => {
      if (itemw.id == item.speedLimitTypeId) {
        tagLists.value = itemw.speedLimitTags
      }
    })
    subForm.value = JSON.parse(JSON.stringify(item))
    visible.value = true
  }
  function updatUserPopTag() {
    tagTypeTitle.value = '编辑方案'
    tagvisible.value = true
  }
  function delLimitTypes(item: any) {
    let delId = ''
    if (item.id) {
      delId = item.id
    } else {
      delId = subFormTag.value.id
    }

    Modal.warning({
      title: '操作确认',
      content: '确定要删除此方案吗？',
      cancelText: '取消',
      titleAlign: 'start',
      hideCancel: false,
      okText: '删除',
      onOk: () => {
        http
          .get({
            url: delLimitType,
            data: () => {
              return {
                id: delId,
              }
            },
          })
          .then((res) => {
            if (res.code == 0) {
              Message.success(res.msg)
              gettypeList()
            } else {
              Message.error(res.msg)
            }
          })
          .catch(console.log)
      },
    })
  }
  function onCopy(item: any) {
    Modal.warning({
      title: '操作确认',
      content: '确定要复制方案吗？',
      cancelText: '取消',
      titleAlign: 'start',
      hideCancel: false,
      okText: '删除',
      onOk: () => {
        http
          .post({
            url: addLimit,
            data: () => {
              return {
                ...item,
              }
            },
          })
          .then((res) => {
            btnLoading.value = false
            if (res.code == 0) {
              Message.success('复制成功')
              visible.value = false
              userForm.value?.resetFields()
              doRefresh()
            } else {
              Message.error(res.msg)
            }
          })
      },
    })
  }
  function onDeleteItem(item: any) {
    Modal.warning({
      title: '操作确认',
      content: '确定要删除此方案吗？',
      cancelText: '取消',
      titleAlign: 'start',
      hideCancel: false,
      okText: '删除',
      onOk: () => {
        http
          .get({
            url: delLimit,
            data: () => {
              return {
                id: item.id,
              }
            },
          })
          .then((res) => {
            if (res.code == 0) {
              Message.success(res.msg)
              doRefresh()
            } else {
              Message.error(res.msg)
            }
          })
          .catch(console.log)
      },
    })
  }
  function delTags(item: any) {
    Modal.warning({
      title: '操作确认',
      content: '确定要删除此标签吗？',
      cancelText: '取消',
      titleAlign: 'start',
      hideCancel: false,
      okText: '删除',
      onOk: () => {
        http
          .get({
            url: delLimitTypeTag,
            data: () => {
              return {
                id: item.id,
              }
            },
          })
          .then((res) => {
            if (res.code == 0) {
              Message.success(res.msg)
              gettypeList(2)
            } else {
              Message.error(res.msg)
            }
          })
          .catch(console.log)
      },
    })
  }
  function handleBeforeOkTags() {
    userFormTags.value?.validate().then((error: any) => {
      if (!error) {
        subFormTags.value.speedLimitTypeId = activeTypeId.value
        if (!subFormTags.value.id) {
          http
            .formpost({
              url: addLimitTypeTag,
              data: () => {
                return {
                  ...subFormTags.value,
                }
              },
            })
            .then((res) => {
              btnLoading.value = false
              if (res.code == 0) {
                Message.success(res.msg)
                addtagvisible.value = false
                subFormTags.value.id = null
                userFormTags.value?.resetFields()
                gettypeList(2)
              } else {
                Message.error(res.msg)
              }
            })
        } else {
          tagsTitle.value = '编辑标签'
          http
            .formpost({
              url: updateLimitTypeTag,
              data: () => {
                return {
                  ...subFormTags.value,
                }
              },
            })
            .then((res) => {
              btnLoading.value = false
              if (res.code == 0) {
                Message.success(res.msg)
                addtagvisible.value = false
                subFormTags.value.id = null
                userFormTags.value?.resetFields()
                gettypeList(2)
              } else {
                Message.error(res.msg)
              }
            })
        }
      }
    })
  }
  function handleBeforeOkTag() {
    console.log(subFormTag.value.id)
    userFormTag.value?.validate().then((error: any) => {
      if (!error) {
        if (subFormTag.value.id) {
          http
            .formpost({
              url: updateLimitType,
              data: () => {
                return {
                  ...subFormTag.value,
                }
              },
            })
            .then((res) => {
              btnLoading.value = false
              if (res.code == 0) {
                Message.success(res.msg)
                tagvisible.value = false
                subFormTag.value.id = null
                userFormTag.value?.resetFields()
                gettypeList(2)
              } else {
                Message.error(res.msg)
              }
            })
        } else {
          http
            .formpost({
              url: addLimitType,
              data: () => {
                return {
                  ...subFormTag.value,
                }
              },
            })
            .then((res) => {
              btnLoading.value = false
              if (res.code == 0) {
                Message.success(res.msg)
                tagvisible.value = false
                subFormTag.value.id = null
                userFormTag.value?.resetFields()
                gettypeList(2)
              } else {
                Message.error(res.msg)
              }
            })
        }
      }
    })
  }
  function handleBeforeOk() {
    userForm.value?.validate().then((error: any) => {
      if (!error) {
        if (!subForm.value.id) {
          http
            .post({
              url: addLimit,
              data: () => {
                return {
                  ...subForm.value,
                }
              },
            })
            .then((res) => {
              btnLoading.value = false
              if (res.code == 0) {
                Message.success(res.msg)
                visible.value = false
                subForm.value.id = null
                userForm.value?.resetFields()
                doRefresh()
              } else {
                Message.error(res.msg)
              }
            })
        } else {
          http
            .post({
              url: updateLimit,
              data: () => {
                return {
                  ...subForm.value,
                }
              },
            })
            .then((res) => {
              btnLoading.value = false
              if (res.code == 0) {
                Message.success(res.msg)
                visible.value = false
                subForm.value.id = null
                userForm.value?.resetFields()
                doRefresh()
              } else {
                Message.error(res.msg)
              }
            })
        }
      }
    })
  }
  function closeNext() {
    showContextMenu.value = false
  }
  watch(
    () => showContextMenu.value,
    () => {
      if (showContextMenu.value) {
        document.body.addEventListener('click', closeNext)
      } else {
        document.body.removeEventListener('click', closeNext)
      }
    },
    { deep: true }
  )
  function onContextMenu(item: any, e: MouseEvent) {
    subFormTag.value = JSON.parse(JSON.stringify(item))
    const { clientX, clientY } = e
    e.preventDefault()
    contextMenuStyle.left = clientX - 200 + 'px'
    contextMenuStyle.top = clientY - 90 + 'px'
    showContextMenu.value = true
  }
  function doRefresh(value?: number) {
    // eslint-disable-next-line prettier/prettier
    tableLoading.value = true
    if (value) {
    }
    http
      .get({
        url: getLimitList,
        data: () => {
          return {
            ...form,
          }
        },
      })
      .then((res) => {
        gettypeList(1)
        tableLoading.value = false
        table.handleSuccess(res.data)
      })
      .catch(console.log)
  }

  const onUpdateBorder = (isBordered: boolean) => {
    tables.bordered = isBordered
  }
  function onUpdateStriped(isStriped: boolean) {
    tables.striped = isStriped
  }
  function rowClassNameFun(_record: any, index: number) {
    return index % 2 === 1 && table.striped.value ? 'table-striped' : null
  }

  onMounted(async () => {
    await gettypeList()
    await doRefresh()
  })
</script>

<style lang="less" scoped>
  :deep(.arco-input-append) {
    padding: 0px;
  }
  .containBox {
    width: 100%;
    display: flex;
    background-color: var(--color-bg-1);
    border-radius: 10px;

    .tabelLeft {
      width: 200px;
      min-height: 100%;
      padding: 24px 20px 20px 20px;
      box-sizing: border-box;
      border-right: 1px solid var(--color-border);

      .tabTitle {
        display: flex;
        justify-content: space-between;
        align-items: center;

        .title {
          color: rgb(var(--gray-10));
          font-size: 16px;
        }

        .btns {
          display: flex;
          justify-content: space-between;
          align-items: center;
        }
      }

      .tagList {
        min-width: 120px;
        margin-top: 20px;
        display: flex;
        flex-wrap: wrap;
        align-items: flex-start;

        .activeTag {
          width: 100%;
          height: 34px;
          text-align: center;

          .actives {
            display: block;
            cursor: pointer;
            width: 100%;
            height: 100%;
            line-height: 34px;
            color: rgb(var(--primary-6));
            background-color: rgb(var(--arcoblue-1));
          }
          .wraps {
            display: inline-block;
            width: 56px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            cursor: pointer;
          }
          .noactive {
            display: block;
            cursor: pointer;
            width: 100%;
            color: rgb(var(--gray-10));
            height: 100%;
            line-height: 34px;
          }
        }
      }
    }
  }

  :deep(.arco-table-cell-expand-icon) {
    justify-content: center;
  }

  :deep(.arco-tabs-nav-tab) {
    background-color: var(--color-bg-1);
    padding: 20px 0;
    border-radius: 10px;
  }

  :deep(.arco-tabs-content::before) {
    height: 0px !important;
  }

  :deep(.arco-tabs-tab) {
    margin: 0 20px;
    padding: 0;
    padding-bottom: 5px;
  }

  :deep(.arco-tabs-nav::before) {
    position: static !important;
    height: 0px !important;
  }

  :deep(.arco-tabs-content) {
    padding-top: 20px;
  }

  .contex-menu-wrapper {
    position: absolute;
    width: 100px;
    z-index: 999;
    list-style: none;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
    background-color: var(--color-bg-1);
    padding-left: 0;
    border-radius: 4px;

    & > li {
      width: 100%;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      padding: 0px 0;

      & button {
        width: 100%;
      }
    }

    & > li:hover {
      color: var(--primary-color);
    }
  }

  .top {
    margin-bottom: 20px;
    background-color: var(--color-bg-1);
    border-radius: 10px;

    .box {
      display: flex;
      align-items: center;
      height: 30px;
      margin: 5px 10px;

      .line {
        width: 5px;
        height: 25px;
        margin-right: 10px;
        background-color: #40a1ff;
      }

      .text {
        font-size: 16px;
        line-height: 25px;
        text-align: center;
      }
    }

    .flex {
      display: flex;
      justify-content: space-around;
      margin: 12px 7px;

      .dataBox:last-child {
        margin-right: 0px !important;
      }

      .dataBox {
        display: flex;
        align-items: center;
        justify-content: center;
        align-items: center;
        flex: 1;
        margin-right: 20px;
        height: 150px;
        background-image: linear-gradient(rgb(var(--arcoblue-1)), #fff);
        border: 1px solid #f2f3f5;
        border-radius: 10px;

        .img {
          width: 70px;
          height: 70px;
          margin-right: 20px;
          border-radius: 50%;
        }

        .name {
          margin-top: 0;
        }

        .number {
          font-size: 22px;
        }
      }
    }
  }
  :deep(.arco-table-td-content button) {
    padding: 0px !important;
    margin-right: 8px;
    background: none !important;
  }
  .avatar-container {
    position: relative;
    width: 30px;
    height: 30px;
    margin: 0 auto;
    vertical-align: middle;

    .avatar {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }

    .avatar-vip {
      border: 2px solid #cece1e;
    }

    .vip {
      position: absolute;
      top: 0;
      right: -9px;
      width: 15px;
      transform: rotate(60deg);
    }
  }

  .gender-container {
    .gender-icon {
      width: 20px;
    }
  }

  .custom-checkbox-card {
    padding: 5px 16px;
    border-radius: 4px;
    width: auto;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    background-color: var(--color-fill-2);
  }

  .custom-checkbox-card-mask {
    height: 14px;
    width: 14px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 2px;
    border: 1px solid var(--color-border-2);
    box-sizing: border-box;
  }

  .custom-checkbox-card-mask-dot {
    width: 8px;
    height: 8px;
    border-radius: 2px;
  }

  .custom-checkbox-card-title {
    color: var(--color-text-1);
    font-size: 14px;
  }

  .custom-checkbox-card:hover,
  .custom-checkbox-card-checked,
  .custom-checkbox-card:hover .custom-checkbox-card-mask,
  .custom-checkbox-card-checked .custom-checkbox-card-mask {
    border-color: rgb(var(--primary-6));
  }

  .custom-checkbox-card:hover .custom-checkbox-card-title,
  .custom-checkbox-card-checked .custom-checkbox-card-title {
    color: rgb(var(--primary-6));
  }

  .custom-checkbox-card-checked .custom-checkbox-card-mask-dot {
    background-color: rgb(var(--primary-6));
  }
</style>
<style scoped>
  .ant-table-striped :deep(.table-striped) td {
    background-color: #fafafa;
  }
</style>
